/*

	*does the classroom observation analysis for the "Making the Grade" paper
		

*/

clear all

#delimit cr
*set trace on
*classroom observations

	global classobs_dir ${base_dir}/Classroom Observations
	use "${dta_dir}/2_Classroom_Obs_Data.dta", clear

	




	
	
*interactions between round and study arm
gen MT_X_Round_1 = MT_Program*(round==1)
gen MT_X_Round_2 = MT_Program*(round==2)
gen MT_X_Round_3 = MT_Program*(round==3)

gen CCT_X_Round_1 = CCT_Program*(round==1)
gen CCT_X_Round_2 = CCT_Program*(round==2)
gen CCT_X_Round_3 = CCT_Program*(round==3)


*interactions between period and study arm
gen MT_X_Period_1 = MT_Program*(period==1)
gen MT_X_Period_2 = MT_Program*(period==2)
gen MT_X_Period_3 = MT_Program*(period==3)

gen CCT_X_Period_1 = CCT_Program*(period==1)
gen CCT_X_Period_2 = CCT_Program*(period==2)
gen CCT_X_Period_3 = CCT_Program*(period==3)


*need even weights for overall regressions
gen all = 1


*do factor analyses
	*note that the factors are already included in the dataset
		factor ta1_refers_to_TG ta2_moves_freely ta3_calls_on_infidividuals ta5_brings_pupils_back ta6_observes_records_performa ta7_lesson_not_planned      ///
				ta10_verylittle_participation ta11_ignores_pupils_off_task ta14_pcttime_ll, factors(3)
		rotate, varimax		
		
		factor par1_sounds par2_letters par3_words par4_sentences par5_whole_class par6_smaller_group par7_individual_atseat par8_individual_atboard ///
			par9_onboard par10_inprimer par11_inreader par12_other par16_pct_pupils_participatin par14_ll [aw=pct_par15_minutes_on_reading], ///
			factors(5)
		rotate, varimax
		
		factor paw1_pictures paw2_letters paw3_words paw4_sentences paw5_name paw6_air_writing paw7_handwriting_practice paw8_copying_tchrtext ///
			paw9_writing_owntext paw10_on_slate paw11_on_paper paw12_on_board paw16_pct_pupils_participatin paw14_ll [aw=pct_paw15_minutes_on_writing], ///
			factors(5)
		rotate, varimax	
		
		factor pasl1_to_partner pasl2_to_small_group pasl3_to_whole_class pasl4_to_teacher ///
			pasl8_pct_pupils_participatin pasl6_ll [aw=pct_pasl7_minutes_on_speaklisten], ///
			factors(2)
		rotate, varimax	

			
	*generate modified lottery strata for pairwise comparisons
	gen group = Group
	
	gen group_C_vs_RC = group
	replace group_C_vs_RC = group + 100 if Study_Arm ==2
	
	gen group_C_vs_FC = group
	replace group_C_vs_FC = group + 100 if Study_Arm ==1

	gen group_RC_vs_FC = group
	replace group_RC_vs_FC = group + 100 if Study_Arm ==0
	
	
				
	compress _all
			
*save analysis dataset
save "${dir}/2_Classroom_Obs_Analysis.dta", replace


*make version that is collapsed to level of a given class observation
	drop par12_other_specify

	egen classobs_id = group(id round)

	preserve

		collapse ta* par* paw* pasl* pct* tf* rl* wl* sll* period, by(classobs_id)

		tempfile collapsed

		save `collapsed'


	restore





	keep classobs_id round group day_of_week enumerator Study_Arm School_Code Group group_C_vs_RC group_C_vs_FC group_RC_vs_FC all MT_Program CCT_Program

	duplicates drop

	isid classobs_id


	merge 1:1 classobs_id using `collapsed', assert(3) nogen

save "${dir}/5_Classroom_Obs_Analysis_collapsed.dta", replace

local versions raw index
foreach version of local versions{

*set varlists depending on version of analysis - raw vars or indices
if "`version'" == "raw"{


		*overall
		local pv_all pct_ta17_minutes_teaching pct_ta16_minutes_inclass_nottea pct_ta15_minutes_outside_class pct_par15_minutes_on_reading pct_paw15_minutes_on_writing pct_pasl7_minutes_on_speaklisten

		*teacher actions
		local tv ta1_refers_to_TG ta7_lesson_not_planned ta6_observes_records_performa ta9_doesnt_callon_individ ta2_moves_freely ta8_remains_at_front ///
			ta4_encourages_participation ta10_verylittle_participation ta5_brings_pupils_back  ta11_ignores_pupils_off_task ta14_pcttime_ll ///
			ta13_pcttime_english ta3_calls_on_infidividuals  ta12_doesnt_record_performanc 
		
		*pupil reading
		local pv_pct_par15_minutes_on_reading par1_sounds par2_letters par3_words par4_sentences par5_whole_class par6_smaller_group par7_individual_atseat ///
			par9_onboard par10_inprimer par11_inreader par14_ll par12_other par13_english par16_pct_pupils_participatin par8_individual_atboard 
		
		*pupil writing
		local pv_pct_paw15_minutes_on_writing paw1_pictures paw2_letters paw3_words paw4_sentences paw5_name paw6_air_writing paw8_copying_tchrtext ///
			paw9_writing_owntext paw10_on_slate paw11_on_paper paw14_ll paw12_on_board paw13_english paw7_handwriting_practice  paw16_pct_pupils_participatin

		*pupil speaking/listening
		local pv_pct_pasl7_minutes_on_speak pasl1_to_partner pasl2_to_small_group pasl3_to_whole_class pasl4_to_teacher pasl5_english pasl6_ll  ///
			pasl8_pct_pupils_participatin
}
else if "`version'" == "index"{


		*overall
		local pv_all pct_ta17_minutes_teaching pct_ta16_minutes_inclass_nottea pct_ta15_minutes_outside_class pct_par15_minutes_on_reading pct_paw15_minutes_on_writing pct_pasl7_minutes_on_speaklisten
		
		*teacher actions
		local tv tf_keeps_students_focused tf_solid_lesson_plan	tf_active_throughout_classroom
		
		*pupil reading
		local pv_pct_par15_minutes_on_reading rl_sounds_letters rl_wholelanguage_on_board rl_basics_breakoutsessions rl_lango_sentences_reader rl_primer_paragraphs		
		
		*pupil writing
		local pv_pct_paw15_minutes_on_writing wl_pictures_words_stories wl_copy_teacher_text wl_leblango_practice_slates wl_picts_ltrs_paper_highenergy wl_lango_sentences_handwriting
		
		*pupil speaking/listening
		local pv_pct_pasl7_minutes_on_speak sll_group_only sll_indiv_tchr_and_group
		
		
}
*end block to set varlists for raw version
	

	*weighted or unweighted?
	local weighted_choices _unwgtd _wgtd
	foreach weighted of local weighted_choices{

		*activities - reading/writing/speaking&listening	
		local activities all pct_par15_minutes_on_reading pct_paw15_minutes_on_writing pct_pasl7_minutes_on_speak
		foreach activity of local activities{

			local tables Pupil Teacher
			foreach table of local tables{
				local table_short=strlower(substr("`table'",1,1))

				
				copy "${code_dir}\blank_template.xlsx" "${dir}/Classobs_clpsd_`table'_`activity'`weighted'_`version'.xlsx", replace
				local col = 1
				
				*pull out var list based on table and activity
				local this_varlist ``table_short'v_`activity''
				if "`table'"=="Teacher"{
					local this_varlist ``table_short'v'
				}
				
				foreach outcome in `this_varlist'{
					quietly{
						

							use "${dir}/5_Classroom_Obs_Analysis_collapsed.dta", clear						
						
						
							*weight by minutes spent on activity, other than calculating the minutes themselves
							local weight
							
							if "`weighted'"=="_wgtd"{
								if "`outcome'"!="`activity'"{
									local weight [aw=`activity']
								}
							}
							else if "`weighted'"=="_unwgtd"{
								if "`outcome'"!="`activity'"{
									drop if `activity'==0
								}
							}						
									
							local ctrls i.group i.round period i.day_of_week i.enumerator
							local ctrls_no_i group round period day_of_week enumerator
							local regressors MT_Program CCT_Program

											
											
							*randomization inference
								local ri_pvals C_vs_FC C_vs_RC RC_vs_FC
								foreach ri_pval of local ri_pvals{
									preserve
										keep Study_Arm group group_`ri_pval' `outcome' `ctrls_no_i' School_Code all `activity'
										local treat_est
										if "`ri_pval'"=="C_vs_FC"{
											local treat_est _b[2.Study_Arm]
										}
										if "`ri_pval'"=="C_vs_RC"{
											local treat_est _b[1.Study_Arm]
										}
										if "`ri_pval'"=="RC_vs_FC"{
											local treat_est (_b[2.Study_Arm]-_b[1.Study_Arm])
										}
										ritest Study_Arm `treat_est', reps(1000) seed(100453) cluster(School_Code) strata(group_`ri_pval') noanalytics force: ///
											reg `outcome' i.Study_Arm `ctrls' `weight', , `ifstatement'
										matrix define p_`ri_pval' =r(p)
										local p_`ri_pval' = p_`ri_pval'[1,1] 					
										
									restore
								}

							
							
							*main regressions
								reg `outcome' `regressors' `ctrls' `weight', cluster(School_Code), `ifstatement'
								
								local b_C_vs_FC = round(_b[MT_Program],0.001)
								local se_C_vs_FC = round(_se[MT_Program],0.001)			
								local b_C_vs_RC = round(_b[CCT_Program],0.001)			
								local se_C_vs_RC = round(_se[CCT_Program],0.001)			
								
							*other stats for table
								local adjr2 = round(e(r2_a),0.001)
								local N = e(N)

								
							*difference between treatment effects
								lincom MT_Program-CCT_Program
								local b_RC_vs_FC = round(r(estimate),0.001)
								local se_RC_vs_FC = round(r(se),0.001)
								


							*control mean and SD
								sum `outcome' if Study_Arm==0 & e(sample)
								local mean = round(r(mean),0.001)
								local sd = round(r(sd),0.001)
								
							*fix decimal places
								local stats ///
									b_C_vs_FC se_C_vs_FC p_C_vs_FC ///
									b_C_vs_RC se_C_vs_RC p_C_vs_RC ///
									adjr2 ///
									b_RC_vs_FC se_RC_vs_FC p_RC_vs_FC ///
									mean sd
									
								foreach stat of local stats{
									local `stat'=round(``stat'',0.001)
									if ``stat''>0 & ``stat''<1 {
										local `stat' 0``stat''
									}
									if ``stat''>-1 & ``stat''<0 {
										local `stat'=-1*``stat''
										local `stat'=round(``stat'',0.001)
										local `stat' -0``stat''
									}		
									if ``stat''==0 {
										local `stat'="0."+"``stat''"
									}			
									if ``stat''==1 {
										local `stat'="``stat''"+".0"
									}					
									local `stat'="``stat''"+"000"
									local `stat'=substr("``stat''",1,strpos("``stat''",".")+3)
											
											
								}
								*end loop over stats
								
						
							*compute significance stars and add to values
								*also put brackets and parentheses on results
								local ri_pvals C_vs_FC C_vs_RC RC_vs_FC
								foreach ri_pval of local ri_pvals{		
									local stars_`ri_pval'=""
									if `p_`ri_pval''<0.1{
										local stars_`ri_pval'="*"
									}
									if `p_`ri_pval''<0.05{
										local stars_`ri_pval'="**"
									}		
									if `p_`ri_pval''<0.01{
										local stars_`ri_pval'="***"
									}					
								
									*tack stars onto difference estimates
									local b_`ri_pval'="`b_`ri_pval''"+"`stars_`ri_pval''"
									
									*parentheses and brackets for SEs and p-values
									local se_`ri_pval'="("+"`se_`ri_pval''"+")"
									local p_`ri_pval'="["+"`p_`ri_pval''"+"]"
									
								
								}
							
							
							preserve
							
								local letter_column = char(`col'+65)

								local output_stats ///
									outcome ///
									b_C_vs_FC se_C_vs_FC p_C_vs_FC ///
									b_C_vs_RC se_C_vs_RC p_C_vs_RC ///
									N adjr2 ///
									b_RC_vs_FC se_RC_vs_FC p_RC_vs_FC ///
									mean sd
								
								local n_stats : word count `output_stats'
								
								keep if _n<=`n_stats'
								keep School_Code
								gen stat = ""
								gen value = ""
								drop School_Code
								
								forvalues j = 1/`n_stats'{
								
									local this_stat : word `j' of `output_stats'
									replace stat = "`this_stat'" if _n==`j'
									replace value = "``this_stat''" if _n==`j'
									
								
								}
								
								
								export excel value using "${dir}/Classobs_clpsd_`table'_`activity'`weighted'_`version'.xlsx" if inrange(_n,1,1), cell(`letter_column'3) sheetmodify

								export excel value using "${dir}/Classobs_clpsd_`table'_`activity'`weighted'_`version'.xlsx" if inrange(_n,2,4), cell(`letter_column'4) sheetmodify
								
								export excel value using "${dir}/Classobs_clpsd_`table'_`activity'`weighted'_`version'.xlsx" if inrange(_n,5,7), cell(`letter_column'9) sheetmodify
								
								export excel value using "${dir}/Classobs_clpsd_`table'_`activity'`weighted'_`version'.xlsx" if inrange(_n,8,9), cell(`letter_column'13) sheetmodify
								
								export excel value using "${dir}/Classobs_clpsd_`table'_`activity'`weighted'_`version'.xlsx" if inrange(_n,10,12), cell(`letter_column'16) sheetmodify
								
								export excel value using "${dir}/Classobs_clpsd_`table'_`activity'`weighted'_`version'.xlsx" if inrange(_n,13,14), cell(`letter_column'21) sheetmodify
							
						
						restore						
						
					}
					*end block to do quietly
					
					*increment column number
					local col = `col'+1
					
				}
				*end loop over variables
				
					
			}
			*end loop over tables (teacher/pupil)
		}
		*end loop over activities (reading/writing/speaking&listening)
	}
	*end loop over weighting choices
}
*end loop over versions

